package com.lkd.http.controller;

import com.lkd.entity.PolicyEntity;

import com.lkd.exception.LogicException;
import com.lkd.http.vo.PolicyReq;
import com.lkd.service.PolicyService;

import com.lkd.vo.Pager;
import com.lkd.vo.PolicyVO;
import com.lkd.vo.VmVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;

@RestController
@RequestMapping("/policy")
public class PolicyController {

    @Autowired
   private PolicyService policyService;



    @GetMapping("/search")
    public Pager<PolicyEntity> search(@RequestParam(value = "pageIndex",required = false,defaultValue = "1") Long pageIndex,
                                      @RequestParam(value = "pageSize",required = false,defaultValue = "10") Long pageSize,
                                      @RequestParam(value = "policyName",required = false,defaultValue = "") String policyName){

            return policyService.search(pageIndex,pageSize,policyName);

    }


    /**
     * 创建策略
     * @param policyReq
     * @return
     * @throws LogicException
     */
    @PostMapping
    public boolean add(@RequestBody PolicyReq policyReq) throws LogicException{

      return  policyService.add(policyReq);
    }

    /**
     *
     * @param policyId
     * @return
     */
    @DeleteMapping("/{policyId}")
    public boolean remove(@PathVariable String policyId){
        return policyService.remove(policyId);
    }

    /**
     *
     * @param policyId
     * @param policyReq
     * @return
     */
    @PutMapping("{policyId}")
    public boolean modifyPolicy(@PathVariable String policyId,
                                @RequestBody PolicyReq policyReq){
        return policyService.modifyPlicy(Integer.valueOf(policyId),policyReq);
    }

    /**
     *
     * @param InnerCode
     * @return
     */
    @GetMapping("/vmPolicy/{InnerCode}")
    public PolicyVO getPolicy(@PathVariable String InnerCode){
        return  policyService.getPolicy(InnerCode);
    }


    /**
     *
     * @param
     * @return
     */
    @GetMapping()
    public ArrayList<PolicyVO> getPolicyList(){  //@RequestBody ApplyPolicyReq req
        return policyService.getPolicyList();
    };

    @GetMapping("/vmList/{id}")
    public Pager<VmVO> getVmList(@PathVariable String id,
                                 @RequestParam(value = "pageIndex",required = false, defaultValue = "1")  Long pageIndex,
                                 @RequestParam(value = "pageSize",required = false, defaultValue = "10") Long pageSize){
        return policyService.getVmList(Integer.valueOf(id),pageIndex,pageSize);
    }


}
